﻿using System.Linq;
using System.Transactions;
using Bootstrap;
using Bootstrap.AutoMapper;
using DogshowRepository;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Dogshow.Linq2SQLRepository.Test
{
    [TestClass]
    public class RaseRepositoryFixtures
    {
        private IRaseRepository m_Cut;

        [ClassInitialize]
        public static void ClassInit(TestContext ctx)
        {
            var model = new DogshowModel();
            if (model.DatabaseExists())
            {
                model.DeleteDatabase();
            }
            model.CreateDatabase();
            model.ExecuteCommand("INSERT INTO FCIGrupe VALUES ('III FCI Grupa', 'Molosi')");
            model.ExecuteCommand("INSERT INTO RasePasa VALUES ('Bulldog', 1, 'Bulldog')");
            model.ExecuteCommand("INSERT INTO RasePasa VALUES ('Bordoška doga', 1, 'DOGUE DE BORDEAUX')");
        }

        [TestInitialize]
        public void TestInit()
        {
            Bootstrapper.With.AutoMapper().Start();
            
        }

        [TestMethod]
        public void Insert()
        {
            using (new TransactionScope())
            {
                // Arrange
                m_Cut = new RaseRepository();
                var rasa = new Model.RasaPsa() { NazivRase = "Bullmastiff",  OriginalniNazivRase = "BULLMASTIFF"};

                // Act
                var id = m_Cut.Sacuvaj(rasa);

                // Assert
                Assert.IsTrue(id > 0);
            }
        }

        [TestMethod]
        public void Update()
        {
            using (new TransactionScope())
            {
                // Arrange
                m_Cut = new RaseRepository();
                var rasa = m_Cut.VratiPoId(1);
                rasa.NazivRase = "Cane Corso";

                // Act
                m_Cut = new RaseRepository();
                m_Cut.Sacuvaj(rasa);

                // Assert
                m_Cut = new RaseRepository();
                rasa = m_Cut.VratiPoId(1);
                Assert.AreEqual("Cane Corso", rasa.NazivRase);
            }
        }

        [TestMethod]
        public void Vrati_Sve()
        {
            m_Cut = new RaseRepository();

            // Act
            var rase = m_Cut.VratiSve();

            // Assert
            Assert.IsTrue(rase.Count() > 0);
        }

        [TestMethod]
        public void Vrati_Po_Id()
        {
            m_Cut = new RaseRepository();
            var rasa = m_Cut.VratiPoId(1);

            Assert.IsNotNull(rasa);
            Assert.AreEqual("Bulldog", rasa.NazivRase);
            Assert.AreEqual("III FCI Grupa", rasa.FciGrupa.Naziv);
        }
    }
}